﻿using DLL.Models;
using DLL.Models.RequestEmployeeModels;
using DLL.Repository;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Deptus.TestEncounter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        private IEmployeeRepository EmployeeRepository;
        private IShiftRepositiry ShiftRepositiry;
        private IJobRepository JobRepository;

        public EmployeeController(IEmployeeRepository employeeRepository, IShiftRepositiry shiftRepositiry, IJobRepository jobRepository)
        {
            EmployeeRepository = employeeRepository;
            ShiftRepositiry = shiftRepositiry;
            JobRepository = jobRepository;
        }


        // GET api/<EmployeeController>/5
        [HttpGet("GetById{id}")]
        public async Task<IActionResult> GetById([FromRoute] Guid id)
        {
            var result = await EmployeeRepository.GetById(id);
            if (result == null)
            {
                return BadRequest();
            }

            return Ok(result);
        }

        // POST api/<EmployeeController>
        [HttpPost("CreateNew")]
        public async Task<IActionResult> CreateNew([FromBody] UpdateEmployee UpdateEmployee)
        {
            try
            {
                Employee newEmployee = new Employee();
                newEmployee.FirstName = UpdateEmployee.FirstName;
                newEmployee.LastName = UpdateEmployee.LastName;
                newEmployee.SurName = UpdateEmployee.SurName;


                var jobPositions = await JobRepository.GetAll();

                if (jobPositions.Where(x => x.Id == UpdateEmployee.JobPosition).ToList().Count == 0)
                    throw new Exception("Нет такой должности");

                newEmployee.JobPosition = UpdateEmployee.JobPosition;

                await EmployeeRepository.Create(newEmployee);

                return Ok(newEmployee);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        [HttpPatch("Update{id}")]
        public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateEmployee updateEmployee)
        {
            try
            {
                var employee = await EmployeeRepository.GetById(id);
                if (employee == null)
                    throw new Exception("Нет такого работника");

                employee.FirstName = updateEmployee.FirstName;
                employee.LastName = updateEmployee.LastName;
                employee.SurName = updateEmployee.SurName;
                employee.JobPosition = updateEmployee.JobPosition;

                await EmployeeRepository.Update(employee);

                return Ok(employee);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        // DELETE api/<EmployeeController>/5
        [HttpDelete("Delete")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await EmployeeRepository.GetById(id);
            if (result == null)
            {
                return BadRequest();
            }
            try
            {
                await EmployeeRepository.Delete(result);
            }
            catch (Exception ex) { return BadRequest(ex.Message); }
            return Ok(result);
        }

        // GET api/<EmployeeController>/5
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll(int? job)
        {
            var result = await EmployeeRepository.GetAll();
            if (result == null)
            {
                return BadRequest();
            }
            if (job != null)
            {
                return Ok(result.Where(e => e.JobPosition.ToString() == job.ToString()));
            }

            return Ok(result);
        }

        [HttpGet("GetAllPositions")]
        public async Task<IActionResult> GetAllPositions()
        {
            try
            {
                var result = await JobRepository.GetAll();
                if (result == null)
                {
                    return BadRequest();
                }

                return Ok(result);
            }
            catch (Exception ex) { return BadRequest(ex.Message); }
        }






        //private Employee FluentShift(UpdateEmployee update)
        //{
        //    return new Employee
        //    {
        //        FirstName = update.FirstName,
        //        LastName = update.LastName,
        //        SurName = update.SurName,
        //        JobPosition = update.JobPosition
        //    };
        //}
    }
}
